package controllers

import (
	"myerp/models"
	"myerp/src"
	"time"
)

//销售管理
type SaleController struct {
	CommonController
}

//添加
func (c *SaleController) Add() {

	if c.Ctx.Request.Method == "POST" {
		var partner, state, province, city, district int
		var zprice float64
		var name, street string
		var err error
		reData := make(map[string]interface{})
		reData["status"] = 0
		partner, err = c.GetInt("partner")
		if err != nil {
			reData["msg"] = "添加销售订单失败1！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		state, err = c.GetInt("state")
		if err != nil {
			reData["msg"] = "添加销售订单失败2！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		province, err = c.GetInt("province", 0)
		if err != nil {
			// reData["msg"] = "添加销售订单失败！"
			// c.Data["json"] = reData
			// c.ServeJSON()
			// return
		}
		city, err = c.GetInt("city", 0)
		if err != nil {
			// reData["msg"] = "添加销售订单失败！"
			// c.Data["json"] = reData
			// c.ServeJSON()
			// return
		}
		district, err = c.GetInt("district", 0)
		if err != nil {
			// reData["msg"] = "添加销售订单失败！"
			// c.Data["json"] = reData
			// c.ServeJSON()
			// return
		}
		zprice, err = c.GetFloat("zprice")
		if err != nil {
			reData["msg"] = "添加销售订单失败3！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		name = c.GetString("name")
		street = c.GetString("street")
		if name == "" || street == "" {
			reData["msg"] = "添加销售订单失败4！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}

		var md models.SaleOrder
		md.Name = name
		md.State = state
		md.Street = street
		md.Partner = partner
		md.Province = province
		md.City = city
		md.District = district
		md.Ctime = time.Now().Unix()
		md.Zprice = zprice
		md.Onum = src.OrderNumber("S")
		md.Goods = c.GetStrings("goods[]")
		md.GoodsNum = c.GetStrings("GoodsNum[]")
		_, err = models.AddSaleOrder(&md)
		if err != nil {
			reData["msg"] = "添加销售订单失败5！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		reData["msg"] = "添加销售订单成功！"
		reData["status"] = 1
		reData["url"] = "/sale/list"
		c.Data["json"] = reData
		c.ServeJSON()
		return

	}
	//获取商品
	gid := c.GetSession("saleorder")
	if gid == nil {
		c.Redirect("/sale/list", 302)
		return
	}
	_, gl := models.GetGoodsList(1, 1000, "my_goods.id in("+gid.(string)+")")
	c.Data["glist"] = gl
	//获取供应商
	l, _ := models.GetClientsCByCtype()
	c.Data["cl"] = l

	c.Layout = "base.html"
	c.TplName = "sale/add.html"
}

//编辑
func (c *SaleController) Edit() {
	var id int
	if c.Ctx.Request.Method == "POST" {
		var state, province, city, district int
		//var zprice float64
		var name, street string
		var err error
		reData := make(map[string]interface{})
		reData["status"] = 0
		id, err = c.GetInt("id")
		if err != nil {
			reData["msg"] = "修改销售订单失败！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}

		state, err = c.GetInt("state")
		if err != nil {
			reData["msg"] = "修改销售订单失败！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		province, err = c.GetInt("province")
		if err != nil {
			reData["msg"] = "修改销售订单失败！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		city, err = c.GetInt("city")
		if err != nil {
			reData["msg"] = "修改销售订单失败！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		district, err = c.GetInt("district")
		if err != nil {
			reData["msg"] = "修改销售订单失败！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		name = c.GetString("name")
		street = c.GetString("street")
		if name == "" || street == "" {
			reData["msg"] = "修改销售订单失败！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		var md models.SaleOrder
		md.Name = name
		md.State = state
		md.Street = street
		md.Province = province
		md.City = city
		md.District = district
		md.Id = id
		err = models.UpdatSaleByID(&md)
		if err != nil {
			reData["msg"] = "修改销售订单失败！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		reData["msg"] = "修改销售订单成功！"
		reData["status"] = 1
		reData["url"] = "/sale/list"
		c.Data["json"] = reData
		c.ServeJSON()
		return
	}
	d, err := models.GetSaleOrderById(id)
	if err != nil {
		c.Redirect("sale/list", 302)
		return
	}
	c.Data["d"] = d
	c.Layout = "base.html"
	c.TplName = "sale/edite.html"
}

//列表
func (c *SaleController) List() {
	//稍后添加具体条件
	c.Data["ActionName"] = "销售列表"

	page, _ := c.GetInt("page", 1)
	count, l := models.GetSaleOrderList(int64(page), 10, "")
	c.Data["List"] = l
	var pages src.Page = src.NewPage(page, 10, int(count), "/sale/list")
	c.Data["page"] = pages.Show()
	c.Layout = "base.html"
	c.TplName = "sale/list.html"
}
